import gradio as gr
import requests

# 定义 API 地址
BASE_URL = "http://106.54.192.233:8080"  # 替换为实际后台地址
UPLOAD_LEVEL_URL = f"{BASE_URL}/uploadLevel"
GET_LEVEL_LIST_URL = f"{BASE_URL}/getLevelList"
GET_ALL_LEVELS_URL = f"{BASE_URL}/getAllLevels"

# Tab 1: 上传 Level 功能
def upload_level(title, author, query, description, code, data, asset_finished, if_reference, overwrite):
    payload = {
        "title": title,
        "author": author,
        "query": query,
        "description": description,
        "code": code,
        "data": data,
        "assetFinished": asset_finished,
        "ifReference": if_reference,
        "overwrite": overwrite,
    }
    try:
        response = requests.post(UPLOAD_LEVEL_URL, json=payload)
        if response.status_code == 200:
            return f"上传成功：{response.json()}"
        else:
            return f"上传失败，状态码：{response.status_code}, 响应：{response.text}"
    except Exception as e:
        return f"上传时发生错误：{e}"

# Tab 2: 获取单个 Level 功能
def get_level(title):
    payload = {"titles": [title]}
    try:
        response = requests.post(GET_LEVEL_LIST_URL, json=payload)
        if response.status_code == 200:
            levels = response.json()
            return f"获取成功：{levels}"
        else:
            return f"获取失败，状态码：{response.status_code}, 响应：{response.text}"
    except Exception as e:
        return f"获取时发生错误：{e}"

# Tab 3: 获取所有 Levels 功能
def get_all_levels():
    try:
        response = requests.get(GET_ALL_LEVELS_URL)
        if response.status_code == 200:
            levels = response.json()
            output_str = "\n".join([f"""ID: {level['id']}, Title: "{level['title']}" """ for level in levels])
            output_str += "\n" +  ",".join([f'''"{level['title']}"''' for level in levels])
            return output_str
        else:
            return f"获取失败，状态码：{response.status_code}, 响应：{response.text}"
    except Exception as e:
        return f"获取时发生错误：{e}"

# 构建 Gradio 界面
with gr.Blocks() as demo:
    with gr.Tab("上传 Level"):
        gr.Markdown("""### 输入信息以上传 Level
                    
                    如果要批量上传方老师json格式的
                    
                    使用lulu_exp/server/upload_level_from_folder.py""")
        with gr.Row():
            title = gr.Textbox(label="Title", placeholder="请输入 Level 标题")
            author = gr.Textbox(label="Author", placeholder="请输入作者")
        query = gr.Textbox(label="Query", placeholder="请输入查询")
        description = gr.Textbox(label="Description", placeholder="请输入描述")
        code = gr.Textbox(label="Code", placeholder="请输入代码")
        data = gr.Textbox(label="Data", placeholder="请输入数据")
        asset_finished = gr.Checkbox(label="Asset Finished", value=False)
        if_reference = gr.Checkbox(label="If Reference", value=False)
        overwrite = gr.Checkbox(label="Overwrite", value=False)
        upload_log = gr.Textbox(label="日志输出", interactive=False)
        upload_button = gr.Button("上传")
        upload_button.click(
            upload_level,
            inputs=[title, author, query, description, code, data, asset_finished, if_reference, overwrite],
            outputs=upload_log
        )
    
    with gr.Tab("获取单个 Level"):
        gr.Markdown("### 输入标题获取 Level 信息")
        single_title = gr.Textbox(label="Title", placeholder="请输入 Level 标题")
        single_log = gr.Textbox(label="日志输出", interactive=False)
        get_single_button = gr.Button("获取")
        get_single_button.click(
            get_level,
            inputs=single_title,
            outputs=single_log
        )

    with gr.Tab("获取所有 Levels"):
        gr.Markdown("### 获取所有 Levels 信息")
        all_levels_log = gr.TextArea(label="所有 Levels 信息", interactive=False)
        get_all_button = gr.Button("获取所有 Levels")
        get_all_button.click(
            get_all_levels,
            outputs=all_levels_log
        )

# 启动 Gradio 界面
demo.launch()
